#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "log.hpp"
#include "protocol.hpp"
using namespace std;
class Client
{
public:
    Client(const string &ip, const uint16_t &port) : _fd(-1), _Sip(ip), _Sport(port) {}
    void init()
    {
        _fd = socket(AF_INET, SOCK_STREAM, 0);
        if (_fd == -1)
        {
            cerr << strerror(errno) << endl;
            exit(1);
        }
        logMessage(NORMAL, "socket success");
    }
    Request parseLine(const string &line)
    {
        int status = 0;
        int i = 0;
        int size = line.size();
        string left, right;
        char op;
        while (i < size)
        {
            switch (status)
            {
            case 0:
            {
                if (isdigit(line[i]))
                    left.push_back(line[i++]);
                else
                {
                    op = line[i];
                    status = 1;
                }
            }
            break;
            case 1:
            {
                ++i;
                status = 2;
            }
            break;
            case 2:
                right.push_back(line[i++]);
                break;
            default:
                break;
            }
        }
        return Request(stoi(left), stoi(right), op);
    }
    void run()
    {
        struct sockaddr_in addr;
        bzero(&addr, 0);
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = inet_addr(_Sip.c_str());
        addr.sin_port = htons(_Sport);
        if (connect(_fd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
        {
            cerr << strerror(errno) << endl;
            exit(2);
        }
        else
        {
            string inbuffer;
            while (true)
            {
                // 读取数据
                string line;
                cout << " please input#\n";
                getline(cin, line);
                Request req = parseLine(line);
                // 序列化
                string req_text;
                bool ret = req.serialize(req_text);
                if (ret == false)
                    logMessage(ERROR, "Client : serialize fail");
                // 加报头
                ret = enLength(req_text);
                if (ret == false)
                    logMessage(ERROR, "Client : enLength fail");
                // 发数据
                send(_fd, req_text.c_str(), req_text.size(), 0);
                // 接收数据
                string resp_str, resp_text;
                if (!recvPackage(_fd, inbuffer, resp_str))
                    continue;
                // 去报头
                ret = deLength(resp_str, resp_text);
                if (ret == false)
                    logMessage(ERROR, "Client : deLength fail");
                // 反序列化
                Response resp;
                ret = resp.deserialize(resp_text);
                if (ret == false)
                    logMessage(ERROR, "Client : deserialize fail");
                cout << "exitcode :" << resp._exitcode << "\tresulet : " << resp._result << endl;
            }
        }
    }
    ~Client()
    {
        close(_fd);
    }

private:
    int _fd;
    string _Sip;
    uint16_t _Sport;
};